package com.tgpms.web.earlierStage.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.entity.TaskJournal;
import com.tgpms.web.earlierStage.service.EarlierTaskJouService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "EarlierTaskJouController", tags = "前期 任务日志相关接口")
@RestController
@RequestMapping(value = "/ev/earlierTaskJou")
public class EarlierTaskJouController {
	
	@Autowired
	private EarlierTaskJouService earlierTaskJouService;
	@Autowired
	private TokenService tokenService;

	@ApiOperation(value = "新增任务日志接口", notes = "新增任务日志接口", httpMethod = "POST")
	@PostMapping(value = "/addTaskJou")
	public Result addTaskJou(@RequestBody TaskJournal taskJou,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(taskJou.getProjectId()) || Common.isEmpty(taskJou.getId())
				|| Common.isEmpty(taskJou.getJournalDesc()) || Common.isEmpty(taskJou.getPersonLiable())
				|| Common.isEmpty(taskJou.getDispute()) || Common.isEmpty(taskJou.getRatio())
				|| Common.isEmpty(taskJou.getStauts()) ) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			taskJou.setInsertUser(insertName);
			result = earlierTaskJouService.addTaskJou(taskJou, result);
			//异步计算父类任务百分比
			new Thread(){
				public void run(){
					earlierTaskJouService.countPercent(taskJou,0);
				}
			}.start();
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	
	@ApiOperation(value = "修改任务日志接口", notes = "修改任务日志接口", httpMethod = "PUT")
	@PutMapping(value = "/updateTaskJou")
	public Result updateTaskJou(@RequestBody TaskJournal taskJou,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(taskJou.getId()) || Common.isEmpty(taskJou.getJournalId())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			taskJou.setModifyUser(insertName);
			result = earlierTaskJouService.updateTaskJou(taskJou, result);
			//异步计算父类任务百分比
			new Thread(){
				public void run(){
					earlierTaskJouService.countPercent(taskJou,1);
				}
			}.start();
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	@ApiOperation(value = "删除任务日志接口", notes = "删除任务日志接口", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteTaskJou")
	public Result deleteTaskJou(@RequestParam(value = "id", required = false) String id,
								@RequestParam(value = "journalId", required = false) String journalId,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(id) || Common.isEmpty(journalId)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			result = earlierTaskJouService.deleteTaskJou(id,journalId, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}
	
//	@PostMapping(value = "/countTaskJous")
//	public void countTaskJous() {
//		earlierTaskJouService.countPercents();
//	}
	
}
